Vue.mixin({
    data: function () {
        return {
            tableSelect:[],
            searchKey:{},
            batch_data:{
                show:false,
                ids:[],
                status:""
            },
            is_loading:false
        }
    },
    methods:{
        formatDate(v) {
            const date = new Date(v*1000);
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();
            let hours = date.getHours();
            let min = date.getMinutes()
            let se = date.getSeconds()

            month = month > 9 ? month : '0' + month;
            day = day > 9 ? day : '0' + day;
            hours = hours > 9 ? hours : '0' + hours;
            min = min > 9 ? min : '0' + min;
            se = se > 9 ? se : '0' + se;

            return `${year}-${month}-${day}`
        },
        setRowClassName({row,index}) {
            if (row.hasChildren) {
                return 'active-row'
            }
        },
        auth(rule){
            if (sessionStorage.ht_admin_rule) {
                return sessionStorage.ht_admin_rule.indexOf(rule) !== -1;
            } else {
                $.ajax({
                    url:'/admin/admin/getUserAuth',
                    dataType:'json',
                    async:false,
                    success:function (res) {
                        if (res.code === 1) {
                            sessionStorage.ht_admin_rule = res.data.auth
                            window.location.reload()
                            return sessionStorage.ht_admin_rule.indexOf(rule) !== -1;
                        }
                    }
                })
            }
            return false
        },
        showSuccessMsg(msg) {
            if (!msg) msg='操作成功'
            this.$message({
                message: msg,
                type: 'success',
                duration: 1000,
            })
        },
        showSuccessMsgCall(fnc,msg='操作成功') {
            if (!msg) msg='操作成功'
            this.$message({
                message: msg,
                type: 'success',
                duration: 1000,
                lockScroll:false,
                onClose:fnc
            })
        },
        showErrorMsgCall(msg,fnc) {
            this.$alert(msg,{
                type:"error",
                callback:fnc,
                lockScroll:false
            })
        },
        showErrorMsg(msg) {
            this.$alert(msg,{
                type:"error",
                lockScroll:false
            })
        },

        confirm(msg,fnc) {
            this.$confirm(msg,'提示',{
                type:'warning',
                callback:function(action, instance){
                    if (action ==="confirm") {
                        fnc()
                    }
                },
            })
        },

        showImg(path,type='image') {
            top.showImg(path,type)
        },

        showLoad() {
            return this.$loading({
                lock:true,
                text:'',
                spinner:'el-icon-loading',
                background:'rgba(0,0,0,0.7)'
            })
        },
        imageOpen(data = 'editData',key='image',count = 1) {
            Helper.open(`/admin/Uploadify/open?data=${data}&callback=imageCallBack&count=${count}&key=${key}`)
        },
        imageCallBack(path,data,key,count) {
            if (count == 1) {
                this[data][key] = path[0]
            } else  {
                if (!this[data][key]) {
                    this[data][key] = path
                } else {
                    this[data][key] = this[data][key].concat(path)
                }
            }
            console.log(data)
            console.log(key)
            console.log(this[data][key])
            Helper.close()
            this.$forceUpdate()
        },
        delPic(data,key,index) {
            this[data][key].splice(index,1)
        },
        // table 多选
        handleSelectChange(val) {
            this.tableSelect = val
        },
        // 批量删除
        batchDel(url,callback = null) {
            let ids = [];
            let that = this;
            if (!that.tableSelect.length) return false;
            that.tableSelect.forEach((item)=>{
                ids.push(item.id)
            })
            that.$confirm('确定删除么?', '提示', {
                type: 'warning',
                lockScroll: false
            }).then(() => {
                axios.post(url, {id: ids}).then((res) => {
                    let result = res.data;
                    if (result.code === 1) {
                        that.showSuccessMsgCall(function () {
                            if (typeof callback == 'function') {
                                callback()
                            } else {
                                that.getIndex()
                            }
                        })
                    } else {
                        that.showErrorMsg(result.msg)
                    }
                })
            })
        },

        batchStatus(row) {
            let ids = row.map(item=>item.id)
            console.log(ids);
            this.$set(this.batch_data,"show",true)
            this.$set(this.batch_data,"ids",ids)
        },
        batchSubmit() {
            Api.request("/admin/lawyer.project_item/batchStatus",vs.batch_data).then(res=>{
                vs.showSuccessMsgCall(()=>{
                    vs.getIndex()
                    vs.$set(vs.batch_data,"show",false)
                })
            })
        },
        // 重置
        reset() {
            this.searchKey = {}
            this.is_search = false
            this.page = 1
            this.getIndex()
        },
        getIndex() {
            this.getData()
        },
        // 搜索
        search() {
            this.is_search = true
            this.getData()
        }
    },
    filters:{
        formatDate(v) {
            const date = new Date(v*1000);
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();
            let hours = date.getHours();
            let min = date.getMinutes()
            let se = date.getSeconds()

            month = month > 9 ? month : '0' + month;
            day = day > 9 ? day : '0' + day;
            hours = hours > 9 ? hours : '0' + hours;
            min = min > 9 ? min : '0' + min;
            se = se > 9 ? se : '0' + se;

            return `${year}-${month}-${day}`
        },
    }
})